package com.holly.unit.deform;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.holly.unit.db.api.pojo.page.PageResult;
import com.holly.unit.deform.api.DeformDataApi;
import com.holly.unit.deform.api.exception.DeformException;
import com.holly.unit.deform.api.exception.enums.DeformExceptionEnum;
import com.holly.unit.deform.api.pojo.base.DesignFormDTO;
import com.holly.unit.deform.api.pojo.online.pojo.DesignFormDataPojo;
import com.holly.unit.deform.api.pojo.rsp.DeformResult;
import com.holly.unit.deform.context.DeformContext;
import com.holly.unit.deform.entity.DesignForm;
import com.holly.unit.deform.entity.DesignFormData;
import com.holly.unit.deform.service.DeformDataService;
import com.holly.unit.query.api.QueryApi;

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 类描述: 表单设计数据操作类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/6 9:50
 */
public class DeformDataOperator implements DeformDataApi<DesignFormData> {

    private DeformDataService deformDataService = DeformContext.deformDataService();

    private QueryApi queryApi = DeformContext.queryApi();


    @Override
    public boolean saveBatchDataByImport(DesignFormDTO designFormDTO, Collection<JSONObject> entityList, String token) {
        DesignForm designForm = new DesignForm();
        BeanUtil.copyProperties(designFormDTO,designForm);
        return deformDataService.saveBatchByImport(designForm,entityList,token);
    }

    @Override
    public DeformResult<PageResult<DesignFormData>> queryPage(DesignFormData designFormData, String desformCode, int pageNo, int pageSize, Map<String, String[]> paramMap, String superQuery, String username) throws UnsupportedEncodingException {
        QueryWrapper<DesignFormData> queryWrapper = queryApi.initQueryWrapper(designFormData, paramMap);
        queryWrapper.orderByDesc("create_time");
        DeformResult<PageResult<DesignFormData>> pageResultDeformResult = deformDataService.queryPage(desformCode, pageNo, pageSize, queryWrapper, superQuery, username);
        return pageResultDeformResult;
    }

    @Override
    public DeformResult<List<DesignFormData>> queryList(DesignFormData designFormData,String desformCode, Map<String,String[]> paramMap, String superQuery, String username) throws UnsupportedEncodingException {
        QueryWrapper<DesignFormData> queryWrapper = queryApi.initQueryWrapper(designFormData, paramMap);
        DeformResult<List<DesignFormData>> listDeformResult = deformDataService.queryList(desformCode, queryWrapper, superQuery, username);
        return listDeformResult;
    }

    @Override
    public List<DesignFormData> selectByMainId(String mainId) {
        return deformDataService.selectByMainId(mainId);
    }

    @Override
    public DeformResult addOne(DesignFormData designFormData) {
        DeformResult deformResult = deformDataService.addOne(designFormData);
        return deformResult;
    }

    @Override
    public DeformResult editOne(DesignFormData designFormData) {
        return deformDataService.editOne(designFormData);
    }

    @Override
    public DeformResult deleteOne(String id, String token) {
        return deformDataService.deleteOne(id,token);
    }

    @Override
    public DeformResult deleteOne(DesignFormData designFormData, String token) {
        return deformDataService.deleteOne(designFormData,token);
    }

    @Override
    public void deleteBatchMain(List<String> asList, String token) {
        if (ObjectUtil.isNull(asList) || asList.size() == 0) {
            throw new DeformException(DeformExceptionEnum.DEFORM_DEL_PARAM_ERROR);
        }
        deformDataService.deleteBatchMain(asList,token);
    }

    @Override
    public DesignFormData getById(String id) {
        DesignFormData entity = deformDataService.getById(id);
        if (ObjectUtil.isNull(entity)) {
            throw new DeformException(DeformExceptionEnum.DEFORM_QUERY_BY_ID_ERROR,id);
        }
        return entity;
    }

    @Override
    public DeformResult extAdd(String desformCode, DesignFormData designFormData) {
        designFormData.setDesformCode(desformCode);
        return deformDataService.addOne(designFormData);
    }

    @Override
    public DeformResult extEdit(String desformCode, DesignFormData designFormData) {
        designFormData.setDesformCode(desformCode);
        return deformDataService.editOne(designFormData);
    }

    @Override
    public DeformResult addFormData(DesignFormDataPojo designFormData) {
        return deformDataService.addFormData(designFormData);
    }

    @Override
    public DeformResult queryFormDataList(DesignFormDataPojo designFormData) {
        return deformDataService.queryFormDataList(designFormData);
    }

    @Override
    public DeformResult delFormData(DesignFormDataPojo designFormData) {
        return deformDataService.delFormData(designFormData);
    }

    @Override
    public DeformResult editFormData(DesignFormDataPojo designFormData) {
        return deformDataService.editFormData(designFormData);
    }

    @Override
    public void deleteBatchForm(List<String> asList, String tableName) {
        deformDataService.deleteBatchForm(asList,tableName);
    }
}
